﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using ParsecParser;
using ParsecParser.Communication;
using ParsecParser.Data;
using ParsecParser.Display;
using ParsecParser.Helpers;
using ParsecClient.Classes;
using Microsoft.Win32;

namespace ParsecClient
{
    /// <summary>
    /// Interaction logic for BackupWindow.xaml
    /// </summary>
    public partial class BackupWindow : Window
    {
        public BackupWindow()
        {
            InitializeComponent();
        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                //mark all checks
                chkCharacter.IsChecked = true;
                chkRaidGroups.IsChecked = true;
                chkSettings.IsChecked = true;
                chkTimers.IsChecked = true;

                //set default export file name
                txtFilePath.Text = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "ParsecSettingsExport.xml");
            }
            catch (Exception ex)
            {
                ((App)Application.Current).HandleError("Error Loading Export", "Error on Window_Loaded", ex, this, true);
            }
        }
        private void btnBrowse_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                //Debug.WriteLine("Load Button Clicked");
                OpenFileDialog openFileDialog = new OpenFileDialog();
                string currentPath = null;
                if (!string.IsNullOrEmpty(txtFilePath.Text))
                {
                    try
                    {
                        currentPath = System.IO.Path.GetDirectoryName(txtFilePath.Text);
                    }
                    catch
                    {
                        currentPath = null;
                    }
                }
                if (!string.IsNullOrEmpty(currentPath) && System.IO.Directory.Exists(currentPath))
                    openFileDialog.InitialDirectory = currentPath;
                else
                    openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
                openFileDialog.RestoreDirectory = true;
                openFileDialog.DefaultExt = "xml";
                openFileDialog.AddExtension = true;
                openFileDialog.FileName = System.IO.Path.GetFileName(txtFilePath.Text);
                openFileDialog.CheckFileExists = false;
                openFileDialog.Filter = "XML files (*.xml)|*.xml";
                openFileDialog.Title = "Export File Path";
                bool? result = openFileDialog.ShowDialog();
                if (result.HasValue && result.Value)
                {
                    txtFilePath.Text = openFileDialog.FileName;
                }
            }
            catch (Exception ex)
            {
                ((App)Application.Current).HandleError("Error", "Error on btnBrowse_Click", ex, this, true);
            }
        }
        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {

            try
            {
                this.Close();
            }
            catch (Exception ex)
            {
                ((App)Application.Current).HandleError("Error", "Error on btnCancel_Click", ex, this, true);
                this.Close();
            }
        }
        private void btnExport_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (string.IsNullOrEmpty(txtFilePath.Text) || !System.IO.Directory.Exists(System.IO.Path.GetDirectoryName(txtFilePath.Text)))
                {
                    ((App)Application.Current).HandleError("Invalid Export Path", null, null, this, false);
                }
                else
                {
                    bool somethingToExport = false;
                    ParsecDataExport export = new ParsecDataExport();
                    if (chkCharacter.IsChecked ?? false)
                    {
                        somethingToExport = true;
                        export.CharacterSettings = new CharacterSettingsCollection() { CharacterSettings = ((App)Application.Current).AllCharacterSettings };
                    }
                    if (chkTimers.IsChecked ?? false)
                    {
                        somethingToExport = true;
                        export.TimerDefs = new TimerDefinitionCollection() { Timers = ((App)Application.Current).GetTimerDefinitions() };
                    }
                    if (chkRaidGroups.IsChecked ?? false)
                    {
                        somethingToExport = true;
                        export.RaidGroups = ((App)Application.Current).RaidGroups;
                    }
                    if (chkSettings.IsChecked ?? false)
                    {
                        somethingToExport = true;
                        export.SettingsExported = true;
                        export.CombatLogFolder = ((ParsecWindow)((App)Application.Current).MainWindow).LogFolderPath;
                        export.ShowFilters = ((ParsecWindow)((App)Application.Current).MainWindow).pnlFilters.Visibility == Visibility.Visible;
                        export.MainWindowHeight = ((ParsecWindow)((App)Application.Current).MainWindow).Height;
                        export.MainWindowWidth = ((ParsecWindow)((App)Application.Current).MainWindow).Width;
                        export.MainWindowTop = ((ParsecWindow)((App)Application.Current).MainWindow).Top;
                        export.MainWindowLeft = ((ParsecWindow)((App)Application.Current).MainWindow).Left;
                        export.LastUsedRaidKey = ((ParsecWindow)((App)Application.Current).MainWindow).CurrentRaidGroup.Name;
                        export.MainTopMost = ((ParsecWindow)((App)Application.Current).MainWindow).Topmost;
                        export.IsStreamMode = ((App)Application.Current).IsStreamMode;
                        export.IsSoftwareRenderingMode = ((App)Application.Current).IsSoftwareRenderingMode;
                        export.MainOpacity = ((ParsecWindow)((App)Application.Current).MainWindow).Opacity;
                        export.RecentFiles = ((App)Application.Current).RecentFiles.Serialize();
                        export.TimerVolume = ((App)Application.Current).VolumeSetting;
                        export.DefaultTimeServer = ((App)Application.Current).TimeServerDefault;
                        export.EnableTimeSync = ((App)Application.Current).TimeSyncEnabled;
                        export.TimeServerHostName = ((App)Application.Current).TimeServerHostNameSetting;
                        export.TimeServerPort = ((App)Application.Current).TimeServerPortSetting;
                        if (((ParsecWindow)((App)Application.Current).MainWindow).ddlRaidReportType.SelectedValue != null && ((ParsecWindow)((App)Application.Current).MainWindow).ddlRaidReportType.SelectedValue is string)
                            ParsecClient.Properties.Settings.Default.RaidDetailsSortKey = (string)((ParsecWindow)((App)Application.Current).MainWindow).ddlRaidReportType.SelectedValue;

                    }
                    if (!somethingToExport)
                    {
                        ((App)Application.Current).HandleError("You must check something to export", null, null, this, false);
                    }
                    else
                    {
                        export.SaveToDisk(txtFilePath.Text);
                        this.Close();
                        ((App)Application.Current).HandleMessage("Export Completed Successfully", null);

                    }
                }
            }
            catch (Exception ex)
            {
                ((App)Application.Current).HandleError("Error", "Error on btnExport_Click", ex, this, true);
            }
        }
    }
}
